using System;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TMFileManager {
    public partial class Options : Form {
        private Properties.Settings settings;
        private ColorScheme colorScheme = new ColorScheme();

        public Options() {
            settings = Properties.Settings.Default;
			colorScheme.ReadXml(Path.Combine(Application.StartupPath, "ColorScheme.xml"));
            InitializeComponent();
            txtDepthLevel.Value = settings.DepthLevel;
            chkShowFreeSpace.Checked = settings.ShowFreeSpace;
            loadColorScheme();
        }

        private void loadColorScheme() {
            lvColors.Items.Clear();
            foreach (ColorScheme.ColorSchemeRow cs in colorScheme._ColorScheme.Select("", "Extension ASC")) {
                ListViewItem item = new ListViewItem(cs.Extension);
                item.BackColor = Color.FromArgb(cs.A, cs.R, cs.G, cs.B);
                lvColors.Items.Add(item);
            }
        }

        private void btnOK_Click(object sender, EventArgs e) {
            settings.DepthLevel = (int)txtDepthLevel.Value;
            settings.ShowFreeSpace = chkShowFreeSpace.Checked;
            settings.Save();
            colorScheme.WriteXml(Path.Combine(Application.StartupPath, "ColorScheme.xml"));
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            settings.Reload();
        }

        private void btnAddColor_Click(object sender, EventArgs e) {
            if (txtExtension.Text != "") {
                ColorScheme.ColorSchemeRow cs = colorScheme._ColorScheme.FindByExtension(txtExtension.Text);
                if (cs != null) {
                    cs.A = btnColor.BackColor.A;
                    cs.R = btnColor.BackColor.R;
                    cs.G = btnColor.BackColor.G;
                    cs.B = btnColor.BackColor.B;
                } else {
                    colorScheme._ColorScheme.AddColorSchemeRow(txtExtension.Text, btnColor.BackColor.A, btnColor.BackColor.R, btnColor.BackColor.G,
                        btnColor.BackColor.B);
                }

                txtExtension.Text = "";
                btnColor.BackColor = SystemColors.Control;

                loadColorScheme();
            }
        }

        private void btnRemoveColor_Click(object sender, EventArgs e) {
            foreach (ListViewItem item in lvColors.CheckedItems) {
                ColorScheme.ColorSchemeRow cs = colorScheme._ColorScheme.FindByExtension(item.Text);
                if (cs != null) {
                    colorScheme._ColorScheme.RemoveColorSchemeRow(cs);
                }
            }
            loadColorScheme();
        }

        private void btnColor_Click(object sender, EventArgs e) {
            ColorDialog dlg = new ColorDialog();
            if (dlg.ShowDialog() == DialogResult.OK) {
                btnColor.BackColor = dlg.Color;
            }
        }

        private void btnReset_Click(object sender, EventArgs e) {
            settings.Reload();
            colorScheme.ReadXml(Path.Combine(Application.StartupPath, "ColorScheme.xml"));
            txtDepthLevel.Value = settings.DepthLevel;
            chkShowFreeSpace.Checked = settings.ShowFreeSpace;
            loadColorScheme();
        }

        private void lvColors_SelectedIndexChanged(object sender, EventArgs e) {
            if (lvColors.SelectedItems.Count > 0) {
                txtExtension.Text = lvColors.SelectedItems[0].Text;
                btnColor.BackColor = lvColors.SelectedItems[0].BackColor;
            }
        }

        private void lvColors_ItemChecked(object sender, EventArgs e) {
            if (lvColors.CheckedItems.Count > 0) {
                btnRemoveColor.Enabled = true;
            } else {
                btnRemoveColor.Enabled = false;
            }
        }
    }
}
